﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ModularAndPlugin.WinApp.Contracts;
using SailHero.Soft.FrameWork.Models;

namespace UI.WinForm.DayDatas
{
    public class DayDatasService
    {
        IDataModule _dataModule;

        public DayDatasService()
        {
            _dataModule = DayDatasCommandFacade.ModuleHandler.DataModule;
        }

        public BaseDayData GetProduct(string id_)
        {
            try
            {
                var source = _dataModule.baseDayDataRepository.GetList(itm => itm.P_ID == id_);
                if (source != null && source.Count() > 0)
                {
                    return source.FirstOrDefault();
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {

            }
        }

        public string GetProductName(string id_)
        {
            try
            {
                var source = _dataModule.baseDayDataRepository.GetList(itm => itm.P_ID == id_);
                if (source != null && source.Count() > 0)
                {
                    return string.Format("{0}", source.FirstOrDefault().ParamValue);
                }
                else
                    return "";
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {

            }
        }

        public double GetProductsPrice(string id_)
        {
            try
            {
                var source = _dataModule.baseDayDataRepository.GetList(itm => itm.P_ID == id_);
                if (source != null && source.Count() > 0)
                {
                    return source.FirstOrDefault().ParamValue;
                }
                else
                    return -1;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {

            }
        }

        public List<BaseDayData> GetProductsDataTable()
        {
            try
            {
                var source = _dataModule.baseDayDataRepository.GetList(itm => !string.IsNullOrEmpty(itm.P_ID));
                if (source != null && source.Count() > 0)
                {
                    return source.ToList();
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {

            }
        }

        public string Insert(string name_, double price_)
        {
            try
            {
                BaseDayData mdl = new BaseDayData() { P_ID = "2222", ParamValue = price_, CollectionDateTime = DateTime.Now, ParamID = name_ };
                _dataModule.baseDayDataRepository.Add(mdl);
                _dataModule.baseDayDataRepository.UnitOfWork.SaveChanges();
                return mdl.P_ID;

            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {

            }
        }

        private int NewID()
        {
            try
            {

                return 0;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {

            }

        }
    }
}